package com.mygy.fitnesshelper;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.content.Intent;
import android.graphics.RenderEffect;
import android.graphics.Shader;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mygy.fitnesshelper.data.Plan;
import com.mygy.fitnesshelper.data.Product;
import com.mygy.fitnesshelper.data.Store;
import com.mygy.fitnesshelper.data.User;
import com.mygy.fitnesshelper.data.exercises.ExercisesStore;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import eightbitlab.com.blurview.BlurView;
import eightbitlab.com.blurview.RenderScriptBlur;

public class MainActivity extends AppCompatActivity {
    public static User user;

    private TextView stepsDoneTV;
    private TextView stepsNeedTV;
    private TextView waterDoneTV;
    private TextView waterNeedTV;
    private TextView waterPercentTV;
    private TextView activityTimeTV;
    private TextView foodProteinsTV;
    private TextView foodFatsTV;
    private TextView foodCarbsTV;
    private TextView foodCcalsTV;
    private TextView planTimeTV;
    private TextView planNameTV;
    private TextView plansNumTV;
    private LinearLayout nearestPlanInfoPanel;

    private static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ExercisesStore.squat.getDescription();
        /////
        ConstraintLayout constraintLayout = findViewById(R.id.mainStart);

        AnimationDrawable animationDrawable = (AnimationDrawable) constraintLayout.getBackground();
        animationDrawable.setEnterFadeDuration(100);
        animationDrawable.setExitFadeDuration(5000);
        animationDrawable.start();
        /////

        ///////
        float radius = 20f;
        View decorView = getWindow().getDecorView();
        ViewGroup rootView = (ViewGroup) decorView.findViewById(android.R.id.content);
        Drawable windowBackground = decorView.getBackground();

        //шаги
        BlurView blurView = findViewById(R.id.blurView);

        blurView.setupWith(rootView, new RenderScriptBlur(this)) // or RenderEffectBlur
                .setFrameClearDrawable(windowBackground) // Optional
                .setBlurRadius(radius);
        blurView.setOutlineProvider(ViewOutlineProvider.BACKGROUND);
        blurView.setClipToOutline(true);

        //трен
        BlurView blurView1 = findViewById(R.id.blurView1);

        blurView1.setupWith(rootView, new RenderScriptBlur(this)) // or RenderEffectBlur
                .setFrameClearDrawable(windowBackground) // Optional
                .setBlurRadius(radius);
        blurView1.setOutlineProvider(ViewOutlineProvider.BACKGROUND);
        blurView1.setClipToOutline(true);

        //актив
        BlurView blurView2 = findViewById(R.id.blurView2);

        blurView2.setupWith(rootView, new RenderScriptBlur(this)) // or RenderEffectBlur
                .setFrameClearDrawable(windowBackground) // Optional
                .setBlurRadius(radius);
        blurView2.setOutlineProvider(ViewOutlineProvider.BACKGROUND);
        blurView2.setClipToOutline(true);

        //вода
        BlurView blurView3 = findViewById(R.id.blurView3);

        blurView3.setupWith(rootView, new RenderScriptBlur(this)) // or RenderEffectBlur
                .setFrameClearDrawable(windowBackground) // Optional
                .setBlurRadius(radius);
        blurView3.setOutlineProvider(ViewOutlineProvider.BACKGROUND);
        blurView3.setClipToOutline(true);

        //питание
        BlurView blurView4 = findViewById(R.id.blurView4);

        blurView4.setupWith(rootView, new RenderScriptBlur(this)) // or RenderEffectBlur
                .setFrameClearDrawable(windowBackground) // Optional
                .setBlurRadius(radius);
        blurView4.setOutlineProvider(ViewOutlineProvider.BACKGROUND);
        blurView4.setClipToOutline(true);

        //план
        BlurView blurView5 = findViewById(R.id.blurView5);

        blurView5.setupWith(rootView, new RenderScriptBlur(this)) // or RenderEffectBlur
                .setFrameClearDrawable(windowBackground) // Optional
                .setBlurRadius(radius);
        blurView5.setOutlineProvider(ViewOutlineProvider.BACKGROUND);
        blurView5.setClipToOutline(true);

        //профиль
        BlurView blurView6 = findViewById(R.id.blurView6);

        blurView6.setupWith(rootView, new RenderScriptBlur(this)) // or RenderEffectBlur
                .setFrameClearDrawable(windowBackground) // Optional
                .setBlurRadius(radius);
        blurView6.setOutlineProvider(ViewOutlineProvider.BACKGROUND);
        blurView6.setClipToOutline(true);


        if(user != null) {
            System.out.println(Store.auth);

            stepsDoneTV = findViewById(R.id.main_stepsDone);
            stepsNeedTV = findViewById(R.id.main_stepsNeed);
            waterDoneTV = findViewById(R.id.mian_waterDone);
            waterNeedTV = findViewById(R.id.main_waterNeed);
            waterPercentTV = findViewById(R.id.main_waterPercent);
            activityTimeTV = findViewById(R.id.main_activityTime);
            foodProteinsTV = findViewById(R.id.main_foodProteins);
            foodFatsTV = findViewById(R.id.main_foodFats);
            foodCarbsTV = findViewById(R.id.main_foodCarbs);
            foodCcalsTV = findViewById(R.id.main_foodCcals);
            planNameTV = findViewById(R.id.main_planName);
            planTimeTV = findViewById(R.id.main_planTime);
            plansNumTV = findViewById(R.id.main_plansNum);
            nearestPlanInfoPanel = findViewById(R.id.main_nearestPlanInfoPanel);


            updateData();

            ImageView profileRoot = findViewById(R.id.main_profileRoot);
            profileRoot.setOnClickListener(v -> {
                Intent intent = new Intent(MainActivity.this, UserProfileActivity.class);
                UserProfileActivity.user = user;
                startActivity(intent);
            });

            TextView programsRoot = findViewById(R.id.main_programsRoot);
            programsRoot.setOnClickListener(v -> {
                Intent intent = new Intent(MainActivity.this, ProgramsAcivity.class);
                ProgramsAcivity.user = user;
                startActivity(intent);
            });

            LinearLayout stepsRoot = findViewById(R.id.main_stepsRoot);
            stepsRoot.setOnClickListener(v -> {
                Intent intent = new Intent(MainActivity.this, StepsActivity.class);
                startActivity(intent);
            });

            LinearLayout waterRoot = findViewById(R.id.main_waterRoot);
            waterRoot.setOnClickListener(v -> {
                Intent intent = new Intent(MainActivity.this, WaterActivity.class);
                startActivity(intent);
            });

            LinearLayout activityRoot = findViewById(R.id.main_activityRoot);
            activityRoot.setOnClickListener(v -> {
                Intent intent = new Intent(MainActivity.this, ActivitiesActivity.class);
                startActivity(intent);
            });

            LinearLayout foodRoot = findViewById(R.id.main_foodRoot);
            foodRoot.setOnClickListener(v -> {
                Intent intent = new Intent(MainActivity.this, FoodActivity.class);
                startActivity(intent);
            });

            LinearLayout plansRoot = findViewById(R.id.main_plansRoot);
            plansRoot.setOnClickListener(v -> {
                Intent intent = new Intent(MainActivity.this, PlansActivity.class);
                startActivity(intent);
            });
        }

        LinearLayout mainStepsRoot = findViewById(R.id.main_stepsRoot);


    }

    @Override
    protected void onResume() {
        super.onResume();
        updateData();
    }

    private void updateData(){
        if(user!= null) {
            Date todayDate = new Date();
            stepsNeedTV.setText(Integer.toString(user.getStepsTarget()));
            stepsDoneTV.setText(Integer.toString(user.getStepsAtDate(todayDate)));

            int waterNeed = user.getWaterTarget();
            int waterDone = user.getWaterAtDate(todayDate);
            waterNeedTV.setText(Integer.toString(waterNeed));
            waterDoneTV.setText(Integer.toString(waterDone));
            waterPercentTV.setText(Integer.toString((int) ((float) waterDone / waterNeed * 100f)) + "%");

            int activitiesTime = 0;
            for (int t : user.getActivitiesAtDate(todayDate).values()) {
                activitiesTime += t;
            }
            activityTimeTV.setText(Integer.toString(activitiesTime));

            Product sum = user.getFoodSumDataAtDate(todayDate);
            foodProteinsTV.setText(String.format("%2.1f", sum.getProteins()));
            foodFatsTV.setText(String.format("%2.1f", sum.getFats()));
            foodCarbsTV.setText(String.format("%2.1f", sum.getCarbs()));
            foodCcalsTV.setText(String.format("%2.1f", sum.getCcals()));


            List<Plan> activePlans = user.getPlansAtDate(todayDate).stream().filter(p -> !p.isDone()).collect(Collectors.toList());
            int activePlansNum = Integer.max(0, activePlans.toArray().length - 1);
            Optional<Plan> nearestPlan = activePlans.stream()
                    .filter(p -> !p.getTime().before(new Date()))
                    .findFirst();
            if (nearestPlan.isPresent()) {
                nearestPlanInfoPanel.setVisibility(View.VISIBLE);
                Plan p = nearestPlan.get();
                planNameTV.setText(p.getName());
                planTimeTV.setText(timeFormat.format(p.getTime()));
            } else {
                nearestPlanInfoPanel.setVisibility(View.INVISIBLE);
                System.out.println("FFFFFFFFFFfff");
            }

            plansNumTV.setText(Integer.toString(activePlansNum));
        }
    }

    public static User getUser() {
        return user;
    }



}